<?php
class Default_Model_Tour extends Zend_Db_Table {
	protected $_name    = 'tours';
	protected $_primary = 'id';
	protected $_dependentTables = array('Default_Model_ContactTour');
	/* protected $_referenceMap = array(
			'Category'=>array(
					'columns' => array('tour_category_id'),
					'refTableClass' => 'Default_Model_TourCategory',
					'refColumns' => array('id'),
					'onDelete' => self::CASCADE,
			)
	
	); */
	
	// count items
	public function countItem($arrParam = null, $options = null) {
		$db = Zend_Registry::get('connectDb');
		$ssFilter = $arrParam['ssFilter'];
		$select = $db->select()
		             ->from('tours AS t', array('COUNT(t.id)  AS totalItem'));
	
		if(!empty($ssFilter['keywords'])){
			$keywords = '%' . $ssFilter['keywords'] . '%';
			$select->where('t.name LIKE ?',$keywords,STRING);
		}
		$result = $db->fetchOne($select);
		return $result;
	
	}
	
	
	// list item
	public function listItem($arrParam = null, $options = null) {
		if($options['task'] == 'admin-list') {
			$db = Zend_Registry::get('connectDb');
			$paginator = $arrParam['paginator'];
			$ssFilter = $arrParam['ssFilter'];
			//$db = Zend_Db::factory($adapter,$config);
			$select = $db->select()
			             ->from('tours AS t', array('id', 'name', 'status', 'order', 'type', 'location_start', 'time_limit'))
			             ->joinLeft('tour_category AS c', 't.tour_category_id = c.id', array('category_name'));
			        
	
			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
	
			if($paginator['itemCountPerPage']>0) {
				$page     = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
	
			// seach keyword
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('t.name LIKE ?',$keywords,STRING);
			}
			$result = $db->fetchAll($select);
		}
	
		return $result;
	}
	
	// get info item
	public function getItem($arrParam = null, $options = null) {
		if(($options['task'] == 'admin-info') || ($options['task'] == 'admin-edit')) {
			$where  = 'id = ' . $arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
	        
			//get contact
			$result['contact'] = $this->getContact($arrParam['id']);
		}
		 
	   
		return $result;
	}
	
	// change status
	public function changeStatus($arrParam = null, $options = null) {
			
		// Multi change
		$cid = $arrParam['cid'];
		if(count($cid) > 0) {
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
				
			$ids   = implode(',', $cid);
			$where = 'id IN (' . $ids . ')';
			$data  =  array('status'=>$status);
			$this->update($data, $where);
		}
			
		// change only one item
		if($arrParam['id'] > 0) {
			$status = $arrParam['type'];
			$data   = array('status'=>$status);
			$where  = 'id = ' . $arrParam['id'];
			$this->update($data, $where);
		}
	}
	
	// orderItems
	public function sortItems($arrParam = null, $options = null) {
		$order = $arrParam['order'];
		$cid   = $arrParam['cid'];
		if(count($cid) > 0) {
			foreach ($cid as $key => $value) {
				$where = 'id = ' . $value;
				$data  = array('order' => $order[$value]);
				$this->update($data, $where);
			}
	
		}
	}
	
	// delete item
	public function deleteItem($arrParam = null, $options = null) {
		if($options['task'] == 'admin-delete') {
			$where = ' id = ' . $arrParam['id'];
			$this->delete($where);
			
		}
		 
		if($options['task'] == 'admin-multi-delete') {
			$cid = $arrParam['cid'];
			if(count($cid) > 0) {
				$ids   = implode(',', $cid);
				$where = 'id IN (' . $ids . ')';
				$this->delete($where);
			}
			
			
		}
	}
	
	// save item
	public function saveItem($arrParam = null, $options = null) {
		
		if($options['task'] == 'admin-add') {
			$row = $this->fetchNew();
			
			$row->name                 = $arrParam['name'];
		    $row->created 		       = date("Y-m-d");
			$row->created_by 	       = 1; 
			$row->status 		       = $arrParam['status'];
			$row->order 		       = $arrParam['order'];
			$row->type                 = $arrParam['type'];
			$row->time_limit 	       = $arrParam['time_limit'];
			$row->location_start       = $arrParam['location_start']; 
			$row->location_end         = $arrParam['location_end'];
		    $row->time_start_apply     = $arrParam['time_start_apply']; 
			$row->time_end_apply       = $arrParam['time_end_apply']; 
			$row->time_start_register  = $arrParam['time_start_register'];
			$row->time_end_register    = $arrParam['time_end_register']; 
			$row->time_available       = $arrParam['time_available']; 
			$row->cost                 = $arrParam['cost'];
			$row->image                = $arrParam['image'];
			$row->tour_category_id     = $arrParam['tour_category_id'];
			$row->notice               = $arrParam['notice'];
			$row->timetable            = $arrParam['timetable'];
			$row->medium               = $arrParam['medium'];
			$row->hotel_iso            = $arrParam['hotel_iso'];
			$row->description          = $arrParam['description'];
			 	
	        $id =  $row->save();
	        
	        // add contact
	        if($arrParam['contact'] != 0) {
		        $arrParam['tours_id']      = $id;
		        $arrParam['contact_id']    = $arrParam['contact'];
		        $contact = new Default_Model_ContactTour();
		        $contact->saveItem($arrParam, array('task' => 'admin-add'));
	        }
	        
	        return $id;
		}
		
	    if($options['task'] == 'admin-add-image') {
	    	$id    = $arrParam['id'];
	    	$where = 'id = ' . $id;
	    	
	    	$row = $this->fetchRow($where);
	    	
	    	$row->image = $arrParam['image'];
	    	$row->save();
	    }
	    
		if($options['task'] == 'admin-edit') {
			$id    = $arrParam['id'];
			$where = 'id = ' . $id;
	
			$row =  $this->fetchRow($where);
	
			$row->name                 = $arrParam['name'];
		    $row->modified 		       = date("Y-m-d");
			$row->modified_by 	       = 1; 
			$row->status 		       = $arrParam['status'];
			$row->order 		       = $arrParam['order'];
			$row->type                 = $arrParam['type'];
			$row->time_limit 	       = $arrParam['time_limit'];
			$row->location_start       = $arrParam['location_start']; 
			$row->location_end         = $arrParam['location_end'];
		    $row->time_start_apply     = $arrParam['time_start_apply']; 
			$row->time_end_apply       = $arrParam['time_end_apply']; 
			$row->time_start_register  = $arrParam['time_start_register'];
			$row->time_end_register    = $arrParam['time_end_register']; 
			$row->time_available       = $arrParam['time_available']; 
			$row->cost                 = $arrParam['cost'];
			$row->tour_category_id     = $arrParam['tour_category_id'];
			$row->notice               = $arrParam['notice'];
			$row->timetable            = $arrParam['timetable'];
			$row->medium               = $arrParam['medium'];
			$row->hotel_iso            = $arrParam['hotel_iso'];
			$row->view                 = $arrParam['view'];
			$row->description          = $arrParam['description'];
	
			$row->save();
			

		    $contactTour = new Default_Model_ContactTour();
			if($arrParam['contact'] > 0){
				$contactTour->saveItem($arrParam, array('task'=>'admin-edit'));
			}else {
				if($arrParam['contact'] == 0){
					$contactTour->deleteItem($arrParam, array('task'=>'admin-delete-tour'));
				}
			}
		
		}
	}
	
	public function getLocation() {
		$location           = array(); 
		$location['start']  = array('Hà Nội', 'TP.HCM', 'Đà Nẵng');
		$location['end']    = array('Hà Nội', 'TP.HCM', 'Đà Nẵng', 'Nha Trang', 'Châu Âu', 'Châu Á');
		return $location;
	}
	
	// get contact
	public function getContact($id) {
		if(isset($id)){
			$tourRowset     = $this->find($id);
			$tour           = $tourRowset->current();
			$contactRowset  = $tour->findDefault_Model_ContactViaDefault_Model_ContactTour();
			$result         = $contactRowset->toArray();
			return $result;
		}
	}
	 
}